package com.waya.web;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.waya.web.browser.manager.WbWebManager;
import com.waya.web.browser.webview.WebDefaultViewClient;

/**
 * js 调用本地方法
 */
public class JsJavaActivity extends AppCompatActivity {

    WebView mWebview;

    TextView beginLoading, endLoading, loading, mtitle;

    WbWebManager mWebManager;

    String mUrl = "http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0417/2736.html";

    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_js_java);

        mWebview = (WebView) findViewById(R.id.webView1);
        beginLoading = (TextView) findViewById(R.id.text_beginLoading);
        endLoading = (TextView) findViewById(R.id.text_endLoading);
        loading = (TextView) findViewById(R.id.text_Loading);
        mtitle = (TextView) findViewById(R.id.title);

        mUrl = "http://104.128.92.40/a.php";

        //mUrl = "https://www.alipay.com";
        //mUrl = "about:blank";
        mWebManager = WbWebManager.getInstance(this);
        mWebManager.setTarget(mWebview)
//                .setCookie(mUrl, "JSEESIONID=0000000000000")
//                .setCookie(mUrl, "jesseid=012145")
                //设置默认setting，可设置自定义setting，需继承WebDefaultSetting
                .setSetting()
                .registerJsObj()
                .setChromClient(getWebChromeClient())
                .setWebClient(getWebViewClient())
                //设置js调用java的回调对象
//                .setCallback(this)
                //加载
                .load(mUrl);
        Log.i(TAG, "onCreate: ===》" + mWebManager.getCookieByUrl(mUrl));
        //mWebManager.setErrorPage("file:///android_asset/error.html");
    }

    WebViewClient getWebViewClient() {
        return new WebDefaultViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        };
    }

    WebChromeClient getWebChromeClient() {
        return new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                System.out.println("标题在这里");
                mtitle.setText("网页标题：" + title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    String progress = newProgress + "%";
                    loading.setText("当前进度：" + progress);
                } else if (newProgress == 100) {
                    String progress = newProgress + "%";
                    loading.setText("加载完成：" + progress);
                }
            }
        };
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (mWebManager.handleKeyEvent(keyCode, event)) {
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    protected void onDestroy() {
        mWebManager.destroy();
        super.onDestroy();
    }
}
